﻿using System;
using System.Data;
using System.Configuration;
using System.ServiceModel.Configuration;
using System.ServiceModel.Channels;
using System.ServiceModel.Security.Tokens;
using System.Net.Security;
using PSE.Framework.Common.Security.Services.Authentication.ClientCredentials;

namespace PSE.Framework.Service.Security.Authentication.ServiceCredentials
{
    public class CustomTcpBindingExtension : Binding
    {
        public CustomTcpBindingExtension()
            : base()
        {

        }

        public override BindingElementCollection CreateBindingElements()
        {
            TcpTransportBindingElement tcpTransport = new TcpTransportBindingElement();

            SymmetricSecurityBindingElement messageSecurity = new SymmetricSecurityBindingElement();

            messageSecurity.EndpointSupportingTokenParameters.SignedEncrypted.Add(new TokenParameters());
            X509SecurityTokenParameters x509ProtectionParameters = new X509SecurityTokenParameters();
            x509ProtectionParameters.InclusionMode = SecurityTokenInclusionMode.Never;
            messageSecurity.ProtectionTokenParameters = x509ProtectionParameters;

            SecurityBindingElement security = SecurityBindingElement.CreateSecureConversationBindingElement(messageSecurity);

            BinaryMessageEncodingBindingElement messageEncoding = new BinaryMessageEncodingBindingElement();

            return new BindingElementCollection(new BindingElement[] { security, messageEncoding, tcpTransport });
        }

        public override string Scheme
        {
            get
            {
                return Uri.UriSchemeNetTcp;
            }
        }
    }

    public class CustomTcpBindingExtensionElement : StandardBindingElement
    {
        public CustomTcpBindingExtensionElement()
        {
        }

        protected override void OnApplyConfiguration(Binding binding)
        {
            
        }

        protected override Type BindingElementType
        {
            get { return typeof(CustomTcpBindingExtension); }
        }
    }

    public class CustomTcpBindingCollectionElement :
        StandardBindingCollectionElement<CustomTcpBindingExtension, CustomTcpBindingExtensionElement>
    {

    }
}
